// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('ReserveSocietyControls');

ReserveSocietyControls.TopicAcordianBehavior = function(element) {
    ReserveSocietyControls.TopicAcordianBehavior.initializeBase(this, [element]);

    this._DivId = null;
}

ReserveSocietyControls.TopicAcordianBehavior.prototype = 
{
    initialize : function() 
    {
        ReserveSocietyControls.TopicAcordianBehavior.callBaseMethod(this, 'initialize');

        var elem = this.get_element();
        this._items = new Array();
        for(var i = 0; i < 50; i++)
        {
            var div = $get(elem.id + "_item" + i);
            if(div)
            {
                this._items[this._items.length] = new ReserveSocietyControls.TopicAcordianItemBehavior(div);
            }
            else
            {
                break;
            }
        }

        this._beginRequestHandler = Function.createDelegate(this, this.shrink);
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this._beginRequestHandler);  
        this._pageLoadedHandler = Function.createDelegate(this, this.grow);
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(this._pageLoadedHandler);  

        for(var i = 0; i < this._items.length; i++)
        {
            this._items[i].grow();
        }
    },

    dispose : function() 
    {
        if(this._beginRequestHandler != null)
        {
            Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this._beginRequestHandler);  
            this._beginRequestHandler = null;
        }
        if(this._pageLoadedHandler != null)
        {
            Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(this._pageLoadedHandler);  
            this._pageLoadedHandler = null;
        }
        ReserveSocietyControls.TopicAcordianBehavior.callBaseMethod(this, 'dispose');
    },
    
    grow : function(sender, e)
    {
        for(var n = 0; n < e.get_panelsUpdated().length; n++)
        {
            for(var i = 0; i < this._items.length; i++)
            {
                if(e.get_panelsUpdated()[n].childNodes.length > 0 && e.get_panelsUpdated()[n].childNodes[0].id == this._items[i].get_elem().id)
                {
                    this._items[i].set_elem(e.get_panelsUpdated()[n].childNodes[0]);
                }
                this._items[i].grow();
                this._items[i].removeAnimated();
            }
        }
    },
    
    shrink : function(sender, e)
    {
        //find if came from topic header control
        var doShink = false;
        var cause = e.get_postBackElement();
        for(var i = 0; i < this._items.length; i++)
        {
            var isChild = cause.id.indexOf(this._items[i].get_elem().id) == 0;

            var isChildOfDiv = cause.id.indexOf(this._items[i].get_elem().id + "_" + this.get_DivId()) == 0;
            if(isChild && !isChildOfDiv)
            {
                window.scrollTo(0,0)
                this._items[i].setAnimated();
                doShink = true;
                break;
            }
            if(isChild && isChildOfDiv)
            {
                this._items[i].get_elem().style.overflow = "hidden";
            }
        }
        if(doShink)
        {
            for(var i = 0; i < this._items.length; i++)
            {
                this._items[i].shrink();
            }
        }
    },
    
    get_DivId : function() {
        return this._DivId;
    },

    set_DivId : function(value) {
        this._DivId = value;
    }
}

ReserveSocietyControls.TopicAcordianItemBehavior = function(element) 
{
    this._elem = element;  
    this._targetHeight = 0;;
    this._interval = 10;
    this._shrinking = false;
    this._growing = false;
    this._timerTime = 50;
}

ReserveSocietyControls.TopicAcordianItemBehavior.prototype =
{
    getInnerHeight : function()
    {
        var total = 0;
        for(var i = 0; i < this._elem.childNodes.length; i++)
        {
            if(this._elem.childNodes[i].offsetHeight)
            {
                total += this._elem.childNodes[i].offsetHeight;
            }
            if(this._elem.childNodes[i].marginTop)
            {
                total += this._elem.childNodes[i].style.marginTop;
            }
            if(this._elem.childNodes[i].marginBottom)
            {
                total += this._elem.childNodes[i].style.marginBottom;
            }
        }
        //alert("Finished " + this._elem.id + "\nHEight = " + total);
        return total;
    },
    
    getHeaderHeight : function()
    {
        var header = this.getHeader();
        return header.offsetHeight;
    },
    
    getHeader : function()
    {
        if(this._elem.childNodes.length == 0)
        {
            //refresh node
            this._elem = $get(this._elem.id);
        }
        for(var i = 0; i < this._elem.childNodes.length; i++)
        {
            if(this._elem.childNodes[i].className == "topic_head" || this._elem.childNodes[i].className == "topic_head_animated")
            {
                return this._elem.childNodes[i];
            }
        }
    },
    
    doAction : function()
    {
        this._elem.style.overflow = "hidden";
        if(this._elem.offsetHeight < this._targetHeight)  
        {
            this._growing = true;
            this.doGrow();
        }          
        else if(this._elem.offsetHeight > this._targetHeight)  
        {
            this._shrinking = true;
            this.doShrink();
        }
    },
    
    shrink : function()
    {
        if(this._growing)
        {
            this.finishGrow();
            this._elem.style.height = this._currentHeight;
        }
        if(this._elem)
        {
            this._elem.visablity = "visible";
            this._targetHeight = this.getHeaderHeight();
            this.doAction();
        }
    },
    
    grow : function()
    {   
        if(this._shrinking)
        {
            this.finishShrink();
            this._elem.style.height = this._currentHeight;
        }
        if(this._elem)
        {
            this._elem.visablity = "visible";
            this._targetHeight = this.getInnerHeight();
            this.doAction();
        }
    },
    
    doGrow : function()
    {
        if(this._growing)
        {
            if(this._elem)
            {
                if(this._elem.offsetHeight < this._targetHeight)
                {
                    var step = (this._targetHeight - this._elem.offsetHeight) * 0.2;
                    if(step < 10)
                        step = 10;
                    if(step > this._targetHeight - this._elem.offsetHeight)
                    {
                        step = this._targetHeight - this._elem.offsetHeight;
                    }
                    this._elem.style.height = this._elem.offsetHeight + step + "px";
                    this._currentHeight = parseInt(this._elem.style.height);
               }
                if(this._elem.offsetHeight < this._targetHeight)
                {
                    if(this._growDelegate == null)
                    {
                        this._growDelegate = Function.createDelegate(this, this.doGrow);
                    }
                    window.setTimeout(this._growDelegate, this._timerTime);
                }
                else
                {
                    if(!(Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version <=6))
                    {
                        this._elem.style.height = "auto";
                    }
                    this.finishGrow();
                    this.getHeader().className = "topic_head";
                }
            }
            else
            {
                this.finishGrow();
            }
        }
    },
    
    finishGrow : function()
    {
        this._growing = false;
    },

    doShrink : function()
    {
        if(this._shrinking)
        {
            if(this._elem)
            {
                if(this._elem.offsetHeight > this._targetHeight)
                {
                    var step = (this._elem.offsetHeight - this._targetHeight) * 0.4;
                    if(step < 10)
                        step = 10;
                    if(step > this._elem.offsetHeight - this._targetHeight)
                    {
                        step = this._elem.offsetHeight - this._targetHeight;
                    }
                    this._elem.style.height = (this._elem.offsetHeight - step) + "px";
                    this._currentHeight = parseInt(this._elem.style.height);
                }
                if(this._elem.offsetHeight > this._targetHeight)
                {
                    if(this._shrinkDelegate == null)
                    {
                        this._shrinkDelegate = Function.createDelegate(this, this.doShrink);
                    }
                    window.setTimeout(this._shrinkDelegate, this._timerTime);
                }
                else
                {
                    if(this._targetHeight != this.getHeaderHeight())
                    {
                        
                        if(!(Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version <=6))
                        {
                            this._elem.style.height = "auto";
                        }
                    }
                    this.finishShrink();
                }
            }
            else
            {
                this.finishShrink();
            }
        }
    },

    finishShrink : function()
    {
        this._shrinking = false;
    },
    
    ensureFinished : function()
    {
        if(this._growing)
        {
            this.finishGrow();
        }
        if(this._shrinking)
        {
            this.finishShrink();
        }
    },

    get_elem : function()
    {
        return this._elem;
    },
    
    set_elem : function(value)
    {
        if(this._currentHeight > 0)
        {
            value.style.height = this._currentHeight + "px";
        }
        this._elem = value;
    },

    set_currentHeight : function(value)
    {
        this._currentHeight = value;
    },

    get_currentHeight : function()
    {
        return this._currentHeight;
    },
    
    setAnimated : function()
    {
        this.getHeader().className = "topic_head_animated";
    },
    
    removeAnimated : function()
    {
        this.getHeader().className = "topic_head";
    }
}
ReserveSocietyControls.TopicAcordianBehavior.registerClass('ReserveSocietyControls.TopicAcordianBehavior', AjaxControlToolkit.BehaviorBase);
ReserveSocietyControls.TopicAcordianItemBehavior.registerClass('ReserveSocietyControls.TopicAcordianItemBehavior', null);
